home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_1_3.zip / NOALPHA.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  13KB  |  385 lines

  1. /****************************************************************************
  2. Module name: NoAlpha.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCOLOR
  8. #undef NOCTLMGR
  9. #undef NOKERNEL
  10. #undef NOMB
  11. #undef NOMENUS
  12. #undef NOMSG
  13. #undef NOSHOWWINDOW
  14. #undef NOUSER
  15. #undef NOWINOFFSETS
  16. #undef NOWINMESSAGES
  17. #undef NOWINSTYLES
  18. #include <windows.h>
  19.  
  20. #include <stdlib.h>
  21.  
  22. #include "supercls.h"
  23. #include "noalpha.h"
  24.  
  25. char _szAppName[] = "NoAlphaApp";
  26.  
  27. HANDLE _hInstance = NULL;  // Our instance handle.
  28.  
  29. // **************************************************************************
  30.  
  31.    // Message number of our message to be sent to superclass window proc.
  32. WORD _SetRangeMsg = WM_NULL, _ValidRangeMsg = WM_NULL;
  33.  
  34.    // Forward reference to various functions.
  35. BOOL NEAR PASCAL RegisterAppWndClass (HANDLE hInstance);
  36. BOOL NEAR PASCAL RegisterNoAlphaWndClass (HANDLE hInstance);
  37. BOOL FAR  PASCAL AboutProc (HWND, WORD, WORD, LONG);
  38. BOOL FAR  PASCAL DlgProc (HWND, WORD, WORD, LONG);
  39. LONG FAR  PASCAL NoAlphaAppWndProc (HWND, WORD, WORD, LONG);
  40.  
  41.  
  42. // ***************************************************************************
  43. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int cmdShow) {
  44.    MSG msg;
  45.    HWND hWnd;
  46.  
  47.    _hInstance = hInstance;
  48.  
  49.    if (hPrevInstance == NULL) {  // First instance of NOALPHA running.
  50.  
  51.       // Register NoAlpha's main window's class.
  52.       if (!RegisterAppWndClass(hInstance))
  53.          return(0);
  54.  
  55.       // Register the NoAlpha class.  This is a superclass based on the
  56.       // standard Window's "EDIT" control.
  57.       if (!RegisterNoAlphaWndClass(hInstance))
  58.          return(0);
  59.  
  60.       // Register two new window messages called, "ValidRange" & "SetRange".
  61.       // If these messages have been registered before, Windows returns the 
  62.       // same values that were returned the first time these messages 
  63.       // were registered.
  64.       _ValidRangeMsg = RegisterWindowMessage("ValidRange");
  65.       _SetRangeMsg   = RegisterWindowMessage("SetRange");
  66.  
  67.    } else {    // Another instance of this application is running.
  68.  
  69.       // Demonstrated here are two different ways of getting the values
  70.       // of the two NoAlpha specific messages:
  71.  
  72.  
  73.       // Get the value associated with message, "ValidRange".
  74.       // Because this message has been previously registered, Windows
  75.       // just returns the same value that was returned the first time
  76.       // RegisterWindowMessage was called.
  77.       _ValidRangeMsg = RegisterWindowMessage("ValidRange");
  78.  
  79.  
  80.       // Get the value associated with the message, "SetRange".
  81.       // Because this is a global variable and another instance of this
  82.       // application is running, we can use the GetInstanceData function
  83.       // to retrieve the value for this message.
  84.       GetInstanceData(hPrevInstance, (NPSTR) &_ValidRangeMsg,
  85.          sizeof(_ValidRangeMsg));
  86.    }
  87.  
  88.  
  89.  
  90.    // Create NoAlpha's application window.
  91.    hWnd = CreateWindow(_szAppName, _szAppName, WS_OVERLAPPEDWINDOW,
  92.       CW_USEDEFAULT, SW_SHOW, CW_USEDEFAULT, CW_USEDEFAULT,
  93.       NULL, NULL, hInstance, 0);
  94.  
  95.    if (hWnd == NULL) return(0);
  96.    ShowWindow(hWnd, cmdShow);
  97.    UpdateWindow(hWnd);
  98.  
  99.    while (GetMessage(&msg, NULL, 0, 0)) {
  100.       TranslateMessage(&msg);
  101.       DispatchMessage(&msg);
  102.    }
  103.    return(0);
  104. }
  105.  
  106.  
  107. // ***************************************************************************
  108. // This function registers NoAlpha's application window.
  109. BOOL near pascal RegisterAppWndClass (HANDLE hInstance) {
  110.    WNDCLASS WndClass;
  111.  
  112.    WndClass.style         = 0;
  113.    WndClass.lpfnWndProc   = NoAlphaAppWndProc;
  114.    WndClass.cbClsExtra    = 0;
  115.    WndClass.cbWndExtra    = 0;
  116.    WndClass.hInstance     = hInstance;
  117.    WndClass.hIcon         = LoadIcon(hInstance, _szAppName);
  118.    WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  119.    WndClass.hbrBackground = COLOR_WINDOW + 1;
  120.    WndClass.lpszMenuName  = _szAppName;
  121.    WndClass.lpszClassName = _szAppName;
  122.    return(RegisterClass(&WndClass));
  123. }
  124.  
  125.  
  126. // ***************************************************************************
  127. // This function processes all messages sent to NoAlpha's application window.
  128.  
  129. LONG FAR PASCAL NoAlphaAppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  130.    BOOL fCallDefProc = FALSE;
  131.    LONG lResult = 0;
  132.    FARPROC fpDlgProc;
  133.  
  134.    switch (wMsg) {
  135.       case WM_DESTROY:
  136.          PostQuitMessage(0);
  137.          break;
  138.  
  139.       case WM_COMMAND:
  140.          switch (wParam) {
  141.          case IDM_ABOUT:
  142.             fpDlgProc = MakeProcInstance(AboutProc, _hInstance);
  143.             DialogBox(_hInstance, "About", hWnd, fpDlgProc);
  144.             FreeProcInstance(fpDlgProc);
  145.             break;
  146.  
  147.          case IDM_DIALOGBOX:
  148.             fpDlgProc = MakeProcInstance(DlgProc, _hInstance);
  149.             DialogBox(_hInstance, "DlgBox", hWnd, fpDlgProc);
  150.             FreeProcInstance(fpDlgProc);
  151.             break;
  152.  
  153.          default:
  154.             break;
  155.          }
  156.          break;
  157.  
  158.       default:
  159.          fCallDefProc = TRUE; break;
  160.    }
  161.  
  162.    if (fCallDefProc)
  163.       lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
  164.  
  165.    return(lResult);
  166. }
  167.  
  168. // ***************************************************************************
  169. // This function processes all messages sent to the About dialog box.
  170.  
  171. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  172.    char szBuffer[100];
  173.    BOOL fProcessed = TRUE;
  174.  
  175.    switch (wMsg) {
  176.  
  177.       case WM_INITDIALOG:
  178.          // Set version static window to have date and time of compilation.
  179.          wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  180.          SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  181.          break;
  182.  
  183.       case WM_COMMAND:
  184.          switch (wParam) {
  185.             case IDOK:
  186.             case IDCANCEL:
  187.                if (HIWORD(lParam) == BN_CLICKED)
  188.                   EndDialog(hDlg, wParam);
  189.                break;
  190.  
  191.             default:
  192.                break;
  193.          }
  194.          break;
  195.  
  196.       default:
  197.          fProcessed = FALSE; break;
  198.    }
  199.    return(fProcessed);
  200. }
  201.  
  202. // ***************************************************************************
  203. // This function processes all messages sent to NoAlpha's dialog box.
  204.  
  205. BOOL FAR PASCAL DlgProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  206.    BOOL fProcessed = TRUE, fInRange;
  207.  
  208.    switch (wMsg) {
  209.       case WM_INITDIALOG:
  210.          // Set valid range in both NoAlpha controls.
  211.          SendDlgItemMessage(hDlg, ID_NOALPHA1, _SetRangeMsg, 0,
  212.             MAKELONG(100, 200));
  213.  
  214.          SendDlgItemMessage(hDlg, ID_NOALPHA2, _SetRangeMsg, 0,
  215.             MAKELONG(-300, 100));
  216.          break;
  217.  
  218.       case WM_COMMAND:
  219.          switch (wParam) {
  220.             case IDOK:
  221.                if (HIWORD(lParam) != BN_CLICKED) break;
  222.  
  223.                // Send message to first NoAlpha control to see if its 
  224.                // value is in range.
  225.                fInRange = (BOOL)
  226.                   SendDlgItemMessage(hDlg, ID_NOALPHA1, _ValidRangeMsg, 0, 0l);
  227.  
  228.                if (!fInRange) {
  229.                   // Display error message to user.
  230.                   MessageBox(hDlg, "Value is out of range.", _szAppName, MB_OK);
  231.  
  232.                   // Set focus to "NoAlpha" control so user can change its value.
  233.                   SetFocus(GetDlgItem(hDlg, ID_NOALPHA1));
  234.                   break;
  235.                }
  236.                
  237.                // Send message to second NoAlpha control to see if its 
  238.                // value is in range.
  239.                fInRange = (BOOL)
  240.                   SendDlgItemMessage(hDlg, ID_NOALPHA2, _ValidRangeMsg, 0, 0l);
  241.  
  242.                if (!fInRange) {
  243.                   // Display error message to user.
  244.                   MessageBox(hDlg, "Value is out of range.", _szAppName, MB_OK);
  245.  
  246.                   // Set focus to "NoAlpha" control so user can change its value.
  247.                   SetFocus(GetDlgItem(hDlg, ID_NOALPHA2));
  248.                   break;
  249.                }
  250.                
  251.                EndDialog(hDlg, wParam);
  252.                break;
  253.  
  254.             case IDCANCEL:
  255.                EndDialog(hDlg, wParam);
  256.                break;
  257.          }
  258.  
  259.       default:
  260.          fProcessed = FALSE; break;
  261.    }
  262.    return(fProcessed);
  263. }
  264.  
  265.  
  266. //****************************************************************************
  267. // Window words for the NoAlpha Super Class.
  268. #define NOALPHA_CBCLSEXTRA    (0)
  269.  
  270. #define NOALPHA_CBWNDEXTRA    (4)
  271. #define GSCWW_LOVALUE         (0)
  272. #define GSCWW_HIVALUE         (2)
  273.  
  274.  
  275.  
  276. LONG FAR PASCAL NoAlphaWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  277.  
  278.  
  279. // ***************************************************************************
  280. // This function registers the NoAlpha class.  This is a superclassed class
  281. // based on the standard Windows "EDIT" control.
  282. BOOL near pascal RegisterNoAlphaWndClass (HANDLE hInstance) {
  283.    WNDCLASS WndClass;
  284.  
  285.    // Retrieve current class information for "EDIT" system global class.
  286.    GetClassInfo(NULL, "EDIT", &WndClass);
  287.  
  288.    // Our new class should have a new name.
  289.    WndClass.lpszClassName = "NoAlpha";
  290.    WndClass.hInstance     = hInstance;
  291.  
  292.    // The following WNDCLASS members are not changed for NoAlpha:
  293.    //      style, hIcon, hCursor, hbrBackground, lpszMenuName.
  294.  
  295.    // Register the new window class.
  296.    return(RegisterSuperClass(
  297.       &WndClass,           // Address of WNDCLASS structure.
  298.       NoAlphaWndProc,      // Address of superclass window procedure.
  299.       NOALPHA_CBCLSEXTRA,  // Number of additional class extra bytes.
  300.       NOALPHA_CBWNDEXTRA   // Number of additional window extra bytes.
  301.       ));
  302. }
  303.  
  304.  
  305. // ***************************************************************************
  306. // This function processes all messages sent to "NoAlpha" windows.
  307.  
  308. LONG FAR PASCAL NoAlphaWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  309.    LONG lResult = 0L;
  310.    BOOL fCallBaseClassWndProc = TRUE;
  311.    char szValue[10];
  312.    int nValue, nLoValue, nHiValue;
  313.  
  314.    // Check if message is the registered message: "SetRange".
  315.    if (wMsg == _SetRangeMsg) {
  316.  
  317.       // Change window extra bytes to reflect new valid range.
  318.       SetSCWindowWord(hWnd, GSCWW_LOVALUE, LOWORD(lParam));
  319.       SetSCWindowWord(hWnd, GSCWW_HIVALUE, HIWORD(lParam));
  320.  
  321.       // Message should not be passed to base class window procedure.
  322.       fCallBaseClassWndProc = FALSE;
  323.    }
  324.  
  325.  
  326.    // Check if message is the registered message: "ValidRange".
  327.    if (wMsg == _ValidRangeMsg) {
  328.  
  329.       // Get value in NoAlpha window.
  330.       GetWindowText(hWnd, szValue, sizeof(szValue));
  331.  
  332.       // Convert number to integer.
  333.       nValue = atoi(szValue);
  334.  
  335.       // Retrieve valid range from window extra bytes.
  336.       nLoValue = GetSCWindowWord(hWnd, GSCWW_LOVALUE);
  337.       nHiValue = GetSCWindowWord(hWnd, GSCWW_HIVALUE);
  338.  
  339.       // Determine if user's values is within legal range.
  340.       lResult = (nLoValue <= nValue) && (nValue <= nHiValue);
  341.  
  342.       // Message should not be passed to base class window procedure.
  343.       fCallBaseClassWndProc = FALSE;
  344.    }
  345.  
  346.    switch (wMsg) {
  347.       case WM_NCCREATE:
  348.          // Set the last 8 class extra bytes to have information
  349.          // about the base class.
  350.          SetSuperClassInfo(hWnd);
  351.          break;
  352.  
  353.       case WM_CREATE:
  354.          // By default, set valid range to -32767 to + 32767
  355.          SetSCWindowWord(hWnd, GSCWW_LOVALUE, -32767);
  356.          SetSCWindowWord(hWnd, GSCWW_HIVALUE, +32767);
  357.          break;
  358.  
  359.       case WM_CHAR:
  360.          // Prohibit message from being sent to base window procedure if
  361.          // wParam is a letter.
  362.          if (wParam >= 'A' && wParam <= 'Z') fCallBaseClassWndProc = FALSE;
  363.          if (wParam >= 'a' && wParam <= 'z') fCallBaseClassWndProc = FALSE;
  364.  
  365.          // If message is a letter, notify user that key is illegal
  366.          // for a NoAlpha window by beeping.
  367.          if (fCallBaseClassWndProc == FALSE) MessageBeep(0);
  368.          break;
  369.  
  370.       default: // Pass other messages to base class window procedure.
  371.          break;
  372.  
  373.    }
  374.  
  375.    if (fCallBaseClassWndProc) {
  376.       // Call the base class window procedure and return its result 
  377.       // to the caller.
  378.       lResult =
  379.          CallWindowProc((FARPROC) GetBCWndProc(hWnd),
  380.                         hWnd, wMsg, wParam, lParam);
  381.    }
  382.  
  383.    return(lResult);
  384. }
  385.